package com.bct.jxc.view;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.bct.jxc.common.CommonFactory;
import com.bct.jxc.common.StringUtil;
import com.bct.jxc.model.User;
import com.bct.jxc.server.UserServer;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.ImageIcon;
/**
 * 更改密码
 * @author ASUS
 *
 */
public class ChangePwdInternalFrame extends JInternalFrame{
	private JTextField loginNameTf;
	private JTextField opasswordTf;
	private JTextField npasswordTf;
	private JTextField repasswordTf;
	private UserServer userServer;
	private String loginName;//登录名


	/**
	 * Create the frame.
	 */
	public ChangePwdInternalFrame() {
		super("更改密码",true,true,true,true);
		setBounds(100, 100, 450, 300);
		
		JPanel panel = new JPanel();
		
		JPanel panel_1 = new JPanel();
		
		JLabel lblNewLabel_1 = new JLabel("旧 密 码：");
		lblNewLabel_1.setFont(new Font("楷体", Font.BOLD, 16));
		panel_1.add(lblNewLabel_1);
		
		opasswordTf = new JTextField();
		opasswordTf.setFont(new Font("宋体", Font.PLAIN, 14));
		opasswordTf.setColumns(25);
		panel_1.add(opasswordTf);
		
		JPanel panel_1_1 = new JPanel();
		
		JLabel lblNewLabel_1_1 = new JLabel("新 密 码：");
		lblNewLabel_1_1.setFont(new Font("楷体", Font.BOLD, 16));
		panel_1_1.add(lblNewLabel_1_1);
		
		npasswordTf = new JTextField();
		npasswordTf.setFont(new Font("宋体", Font.PLAIN, 14));
		npasswordTf.setColumns(25);
		panel_1_1.add(npasswordTf);
		
		JPanel panel_1_1_1 = new JPanel();
		
		JLabel lblNewLabel_1_1_1 = new JLabel("确认密码：");
		lblNewLabel_1_1_1.setFont(new Font("楷体", Font.BOLD, 16));
		panel_1_1_1.add(lblNewLabel_1_1_1);
		
		repasswordTf = new JTextField();
		repasswordTf.setFont(new Font("宋体", Font.PLAIN, 14));
		repasswordTf.setColumns(25);
		panel_1_1_1.add(repasswordTf);
		
		JPanel panel_2 = new JPanel();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(panel, GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
				.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
				.addComponent(panel_1_1, GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
				.addComponent(panel_1_1_1, GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
				.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panel_1_1, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panel_1_1_1, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
					.addGap(34)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(61, Short.MAX_VALUE))
		);
		
		JButton updateTf = new JButton("修改");
		updateTf.setIcon(new ImageIcon(ChangePwdInternalFrame.class.getResource("/com/bct/jxc/images/change.png")));
		updateTf.setFont(new Font("楷体", Font.BOLD, 16));
		updateTf.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//非空验证
				if(StringUtil.isEmpty(loginNameTf.getText().trim())) {
					JOptionPane.showMessageDialog(null, "登录名不能为空！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(StringUtil.isEmpty(opasswordTf.getText().trim())) {
					JOptionPane.showMessageDialog(null, "旧密码不能为空！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(!opasswordTf.getText().trim().equals(LoginFrame.getLoginUser().getPassword())) {
					JOptionPane.showMessageDialog(null, "旧密码输入错误！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(StringUtil.isEmpty(npasswordTf.getText().trim())) {
					JOptionPane.showMessageDialog(null, "新密码不能为空！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(npasswordTf.getText().trim().equals(LoginFrame.getLoginUser().getPassword())) {
					JOptionPane.showMessageDialog(null, "新密码不能和旧密码一样！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(!repasswordTf.getText().trim().equals(npasswordTf.getText().trim())) {
					JOptionPane.showMessageDialog(null, "两次密码输入不一致！","警告",JOptionPane.WARNING_MESSAGE);
					return;
				}
				User o = new User();
				o.setLoginName(loginNameTf.getText().trim());;
				o.setPassword(npasswordTf.getText().trim());
				boolean isFlag = userServer.update(o);
				if(isFlag==true) {
					JOptionPane.showMessageDialog(null, "密码修改成功！","提示",JOptionPane.INFORMATION_MESSAGE);
					setNull();
				}else {
					JOptionPane.showMessageDialog(null, "密码修改失败！","警告",JOptionPane.WARNING_MESSAGE);
				}
				
			}
		});
		panel_2.add(updateTf);
		
		JButton resetBtn = new JButton("重置");
		resetBtn.setIcon(new ImageIcon(ChangePwdInternalFrame.class.getResource("/com/bct/jxc/images/cancel.png")));
		resetBtn.setFont(new Font("楷体", Font.BOLD, 16));
		resetBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				setNull();
			}
		});
		panel_2.add(resetBtn);
		
		JLabel lblNewLabel = new JLabel("登 录 名：");
		lblNewLabel.setFont(new Font("楷体", Font.BOLD, 16));
		panel.add(lblNewLabel);
		
		loginNameTf = new JTextField();
		loginNameTf.setFont(new Font("宋体", Font.PLAIN, 14));
		panel.add(loginNameTf);
		loginNameTf.setColumns(25);
		getContentPane().setLayout(groupLayout);
		pack();
		setVisible(true);
		//获取登录名
		loginName = LoginFrame.getLoginUser().getLoginName();
		loginNameTf.setText(loginName);
		userServer = CommonFactory.getUserServer();
	}
	/**
	 * 清空数据
	 */
	private void setNull() {
		opasswordTf.setText("");
		npasswordTf.setText("");
		repasswordTf.setText("");
		
	}
}
